package com.fxsh.learn.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.write.metadata.WriteSheet;
import lombok.extern.slf4j.Slf4j;
import java.util.*;

@Slf4j
public class DemoDataListener extends AnalysisEventListener<DemoData> {
	private ExcelWriter excelWriter;
	private Type type;
	private WriteSheet writeSheet;

	public DemoDataListener(ExcelWriter excelWriter,Type type) {
		writeSheet = EasyExcel.writerSheet(type.getValue()).build();
		this.excelWriter = excelWriter;
		this.type = type;
	}

	/**
	 * 这个每一条数据解析都会来调用
	 *
	 * @param data
	 *            one row value. Is is same as {@link AnalysisContext#readRowHolder()}
	 * @param context
	 */
	@Override
	public void invoke(DemoData data, AnalysisContext context) {
		int line = context.readRowHolder().getRowIndex();
		if (line % 10 == type.getKey()){
			excelWriter.write(Arrays.asList(data), writeSheet);
		}
	}

	@Override
	public void doAfterAllAnalysed(AnalysisContext analysisContext) {
		log.info("所有数据解析完成！" );

	}
	public enum Type{
		ACTUAL(0,"实际"),BUDGET(4, "预算"),DIFF(8,"差异");

		int key;
		String value;

		Type(int key,String value) {
			this.value = value;
			this.key = key;
		}

		public String getValue() {
			return value;
		}

		public int getKey(){
			return key;
		}
	}
}